//
//  ipViewController.m
//  进来还
//
//  Created by 沙少盼 on 16/11/17.
//  Copyright © 2016年 沙少盼. All rights reserved.
//

#import "ipViewController.h"
#import "PaintingViewController.h"
//#import "chatViewController.h"

@interface ipViewController ()<GCDAsyncSocketDelegate>
@property (strong, nonatomic) IBOutlet UITextField *ip;
@property (strong, nonatomic) IBOutlet UITextField *port;
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (nonatomic,strong)GCDAsyncSocket *socket;
@end

@implementation ipViewController
- (IBAction)linkin{
    if (_ip.text.length == 0) {
        [self aleart:@"请输入服务器ip"];
        return;
    }
    if (_port.text.length == 0) {
        [self aleart:@"请输入端口号"];
        return;
    }
    if (_name.text.length == 0) {
        [self aleart:@"请输入一个名字"];
        return;
    }
    if ([_socket isConnected]) {
        PaintingViewController *chatVC = [[PaintingViewController alloc]init];
        chatVC.socket = _socket;
        chatVC.name = _name.text;
        [self.navigationController pushViewController:chatVC animated:YES];
        return;
    }
    NSString *host = _ip.text;
    //3.
    int port = [_port.text intValue];
    //创建socket对象
//    _socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    _socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    //代理耗时操作就用全局队列,如果没有,就用主队列
    NSError *error;
    //建立链接
    [_socket connectToHost:host onPort:port error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
}
- (void)aleart:(NSString *)aleartStr{
    UIAlertController *aleart = [UIAlertController alertControllerWithTitle:@"提示" message:aleartStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [aleart addAction:action];
    [self showDetailViewController:aleart sender:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登入";
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
    NSLog(@"%s",__func__);
    NSLog(@"链接%zd端口成功",port);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [sock readDataWithTimeout:-1 tag:100];
        PaintingViewController *chatVC = [[PaintingViewController alloc]init];
        chatVC.socket = _socket;
        chatVC.name = _name.text;
        [self.navigationController pushViewController:chatVC animated:YES];
    });
}
#pragma marks - 链接断开
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
    if (err) {
        dispatch_async(dispatch_get_main_queue(), ^{
//            [self aleart:@"已退出群聊,请重新登录"];
        });
    }
}
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    NSLog(@"%s",__func__);
    //发送成功之后,需要手动读取数据
    [sock readDataWithTimeout:-1 tag:tag];
}
#pragma marks - 接收到服务器发来的消息
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getPointArrayDict" object:nil userInfo:@{@"data" : dict}];
    //每次接收完数据之后,继续监听数据流
    [sock readDataWithTimeout:-1 tag:tag];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_ip resignFirstResponder];
    [_port resignFirstResponder];
    [_name resignFirstResponder];
}

@end
